#include "getfile.h"

QFileInfoList GetFile::getAll(QString path)
{
    //QDirIterator dir_iterator(path,QDir::Files|QDir::Dirs|QDir::NoDotAndDotDot| QDir::NoSymLinks,QDirIterator::Subdirectories);
    QDirIterator dir_iterator(path,QDir::Files|QDir::Dirs|QDir::NoDotAndDotDot ,QDirIterator::Subdirectories);
    QFileInfoList result;


    while(dir_iterator.hasNext())
    {
        dir_iterator.next();
        result.append( dir_iterator.fileInfo());
    }
    return  result;

}

QFileInfoList GetFile::getAllFile(QString path)
{

    QDirIterator dir_iterator(path,QDir::Files| QDir::NoSymLinks,QDirIterator::Subdirectories);
    QFileInfoList result;
    while(dir_iterator.hasNext())
    {
        dir_iterator.next();
        QFileInfo file_info = dir_iterator.fileInfo();
        result.append(file_info);
    }
    return  result;

}

QList<QString> GetFile::getDir(QString path)
{

    QDirIterator dir_iterator(path,QDir::Dirs |QDir::NoDotAndDotDot| QDir::NoSymLinks,QDirIterator::Subdirectories);
    QList<QString> result;
    while(dir_iterator.hasNext())
    {
        dir_iterator.next();
        result.append(dir_iterator.fileInfo().absoluteFilePath());
    }

    return  result;


}

int GetFile::getnum1(QString source, QString destination)
{
    int sum = 0;
    GetMD5 md5;
    QDir dir;
    QFileInfoList fileInfoList = getAllFile(source);
    QList<QString> dirList = getDir(source);
    foreach (QString str, dirList) {
        QString newpath = str.replace(source, destination);
        if (!dir.exists(newpath)) {
            sum += 4096;
        }
    }

    foreach (QFileInfo info, fileInfoList) {
        QString temp;
        QString newpath = info.absoluteFilePath();
        temp = info.absoluteFilePath().replace(source, destination);
        if (!QFile(temp).exists() || QString::compare(md5.getFileMd5(temp),md5.getFileMd5(newpath))) {
            sum += QFile(newpath).size();
        }
    }
    return sum;
}

int GetFile::getnum2(QString path1, QString path2)
{
    int sum = 0;
    QDir dir;
    QFileInfoList fileInfoList = getAllFile(path1);
    QFileInfoList fileInfoList2 = getAllFile(path2);
    QList<QString> dirList = getDir(path1);
    QList<QString> dirList2 = getDir(path2);
    foreach (QString str, dirList) {
        QString newpath = str.replace(path1, path2);
        /*if (QDir(newpath).exists()) {
            QDir(newpath).removeRecursively();
        }*/
        if (!dir.exists(newpath)) {
            sum += 4096;
        }
    }

    foreach (QFileInfo info, fileInfoList) {
        QString temp;
        temp = info.absoluteFilePath().replace(path1, path2);
        if (!QFile(temp).exists()) {
            //dir.remove(temp);
            sum += info.size();
        }
    }

    foreach (QString str, dirList2) {
        QString newpath = str.replace(path2, path1);
        /*if (QDir(newpath).exists()) {
            QDir(newpath).removeRecursively();
        }*/
        if (!dir.exists(newpath)) {
            sum += 4096;
        }
    }

    foreach (QFileInfo info, fileInfoList2) {
        QString temp;
        temp = info.absoluteFilePath().replace(path2, path1);
        if (!QFile(temp).exists()) {
            //dir.remove(temp);
            sum += info.size();
        }
    }
    return sum;
}
